package com.lantosec.hmrp.modules.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.modules.category.entity.AssetsDirectory;
import com.lantosec.hmrp.modules.category.entity.AssetsInfo;
import com.lantosec.hmrp.modules.category.mapper.AssetsDirectoryMapper;
import com.lantosec.hmrp.modules.category.service.IAssetsCategoryService;
import com.lantosec.hmrp.modules.category.service.IAssetsDirectoryService;
import com.lantosec.hmrp.modules.category.vo.AssetsDirectoryVO;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 资产目录 服务实现类
 * </p>
 *
 * @author AutoCode
 * @since 2020-03-31
 */
@Service
@Primary
@Log4j2
public class AssetsDirectoryServiceImpl extends ServiceImpl<AssetsDirectoryMapper, AssetsDirectory> implements IAssetsDirectoryService {

    @Autowired
    private IAssetsCategoryService categoryService;

    @Override
    public Page<AssetsDirectory> queryPage(PageVO<AssetsDirectory> page, AssetsDirectoryVO condition) {
        PageVO<AssetsDirectory> param = baseMapper.selectPage(page, new QueryWrapper<AssetsDirectory>()
                .eq(StringUtils.isNotEmpty(condition.getDirId()), "dir_id", condition.getDirId())
                .like(StringUtils.isNotEmpty(condition.getAssetsName()), "assets_name", condition.getAssetsName()));
        param.getRecords().forEach(it->{
            String categoryId = it.getCategoryId();
            if(StringUtils.isNotEmpty(categoryId)){
                String categoryName = categoryService.getById(categoryId).getCategoryName();
                it.setCategoryName(categoryName);
            }
        });
        return page.setRecords(param.getRecords());
    }

    @Override
    public String create(AssetsInfo info) {
        AssetsDirectory entity = new AssetsDirectory();
        //目录ID
        String dirId = baseMapper.selectMaxDirId();
        if(StringUtils.isEmpty(dirId)){
            dirId = "LT0001";
        }
        entity.setDirId(dirId);
        //资产名称
        if(StringUtils.isNotEmpty(info.getAssetsName())){
            entity.setAssetsName(info.getAssetsName());
        }
        //资产类别
        if(StringUtils.isNotEmpty(info.getCategoryId())){
            entity.setCategoryId(info.getCategoryId());
        }
        //标签材质
        entity.setLableType(info.getLableType());
        //是否手工录入
        entity.setManualFlag(1);
        //规格型号
        if(StringUtils.isNotEmpty(info.getSpecification())){
            entity.setSpecification(info.getSpecification());
        }
        //计量单位
        if(StringUtils.isNotEmpty(info.getUnit())){
            entity.setSpecification(info.getUnit());
        }
        //资产原价
        entity.setPrice(info.getPrice());
        //供应商编码
        if(StringUtils.isNotEmpty(info.getSupplierCode())){
            entity.setSupplierCode(info.getSupplierCode());
        }
        //供应商名称
        if(StringUtils.isNotEmpty(info.getSupplier())){
            entity.setSupplier(info.getSupplier());
        }
        //生产厂家编码
        if(StringUtils.isNotEmpty(info.getFactoryCode())){
            entity.setFactoryCode(info.getFactoryCode());
        }
        //生产厂家
        if(StringUtils.isNotEmpty(info.getFactory())){
            entity.setFactory(info.getFactory());
        }
        baseMapper.insert(entity);
        return dirId;
    }

    @Override
    public List fuzzyQuery(String assetsName) {
        List<AssetsDirectory> name = baseMapper.selectList(new QueryWrapper<AssetsDirectory>()
                .like(StringUtils.isNotEmpty(assetsName), "assets_name", assetsName));
        return name;
    }
}
